package com.cos.shop.web.servlet;

import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseCode;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.entity.Orders;
import com.cos.shop.entity.User;
import com.cos.shop.entity.vo.ItemVo;
import com.cos.shop.service.*;
import com.cos.shop.service.impl.*;
import com.cos.shop.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: OrderController
 * Author: saber
 * Date:2022/9/20 12:27
 */
@WebServlet("/orders")
public class OrderController extends BaseServlet{
    private OrdersServices os = new OrdersServicesImpl();
    private AddressServices as = new AddressServicesImpl();
    private CartServices cs = new CartServicesImpl();
    private ItemServices is = new ItemServicesImpl();

    public String getOrderView(HttpServletRequest request, HttpServletResponse response){

        User user = (User) request.getSession().getAttribute(Const.SystemConst.IS_LOGIN);

        ResponseResult address = as.getAddressByUser(user.getUid());

        ResponseResult cartList = cs.getCartList(user.getUid());

        request.setAttribute("address",address);
        request.setAttribute("cartList",cartList);
        return Const.FORWARD+"/order.jsp";
    }

    public String addOrder(HttpServletRequest request, HttpServletResponse response){
        int aid = StringUtils.stringToInt(request.getParameter("aid"));
        User user = (User) request.getSession().getAttribute(Const.SystemConst.IS_LOGIN);

        ResponseResult rr = os.addOrders(aid,user.getUid());

        if (rr.getCode()== ResponseCode.SUCCESS.getCode()) {
            request.setAttribute("rr",rr);
            return Const.FORWARD+"/orderSuccess.jsp";
        }else {
            request.setAttribute("rr",rr);
            return Const.FORWARD+"/message.jsp";
        }
    }

    public String getOrderList(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.SystemConst.IS_LOGIN);

        ResponseResult rr = os.getAllOrders(user.getUid());

        request.setAttribute("rr",rr);
        return Const.FORWARD+"/orderList.jsp";
    }

    public String getOrderDetail(HttpServletRequest request, HttpServletResponse response){

        String oid = request.getParameter("oid");

        Orders orders = os.getOrdersDetailByOid(oid);

        List<ItemVo> items = is.getOrdersDetailByOid(oid);

        request.setAttribute("orders",orders);
        request.setAttribute("items",items);

        return Const.FORWARD+"/orderDetail.jsp";
    }

    public String changeStatus(HttpServletRequest request, HttpServletResponse response){
        String oid = request.getParameter("oid");

        ResponseResult rr = os.chageStatus(oid);

        request.setAttribute("rr", rr);

        return Const.FORWARD+"/message.jsp";
    }



} 
